# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# The jquery comboselect plugin: http://plugins.jquery.com/project/comboselect
# Jason Huck: http://devblog.jasonhuck.com/2008/04/25/jquery-combo-select-redux/
# Packaged as a tw.jquery widget by Ciaran Farrell <cfarrell1980@gmail.com>

from tw.api import Widget, JSLink, CSSLink, js_function, js_callback
from tw.jquery.base import *

jQuery = js_function('$')

jquery_rating_js = JSLink(
        filename='static/javascript/jquery.rating.pack.js',
        javascript=[jquery_js],
        modname=__name__)

jquery_rating_css = CSSLink(
        filename='static/css/jquery.rating.css',
        modname=__name__)

class Rating(Widget):
    params =   ["selector", "readonly", "action", "disabled", "selected"]
    javascript=[jquery_rating_js]
    css=[jquery_rating_css]
    action="null"
    readonly=False
    disabled=False
    selected=None

    def __init__(self,id=None,parent=None,children=[],**kw):
        if kw.has_key('action'):
            self.action = kw['action']
        if kw.has_key('selected'):
            self.selected = str(kw['selected'])
        super(Rating,self).__init__(id,parent,children,**kw)

    def update_params(self,d):
        super(Rating,self).update_params(d)
        call="""$(function(){
 $('.auto-submit-star').rating({
  callback: function(value, link){
   // 'this' is the hidden form element holding the current value
   // 'value' is the value selected
   // 'element' points to the link element that received the click.
   // To submit the form automatically:
   //alert(value);
   //this.form.submit();
   // To submit the form via ajax:
   //$(this.form).ajaxSubmit();
  }
 });
});"""
        self.add_call(call)
